package cn.hd.service.impl;

import cn.hd.dao.ProductLevelDao;
import cn.hd.entity.License;
import cn.hd.entity.ProductLevel;
import cn.hd.entity.Productdetail;
import cn.hd.service.ProductLevelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class ProductLevelServiceImpl implements ProductLevelService {
    @Autowired
    private ProductLevelDao dao;

    //查询所有
    @Override
    public PageInfo<ProductLevel> selectProductLevelAll(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ProductLevel> list = dao.selectProductLevelAll();
        for (ProductLevel p : list) {
            if (p.getParentid()!=0){
                long a = p.getParentid();
                System.out.println(p.getParentid());
                ProductLevel pro = dao.selectLevelById(p.getParentid());
                p.setParentname(pro.getName());
            }else {
                p.setParentname("0");
            }

        }
        PageInfo<ProductLevel> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public List<ProductLevel> selectProductLevelOne() {
        return dao.selectProductByLevel(1);
    }

    @Override
    public List<ProductLevel> selectProductLevelTwo() {
        return dao.selectProductByLevel(2);
    }

    @Override
    public PageInfo<Productdetail> selectProductByOne(int pageNum, int pageSize,String name) {
            PageHelper.startPage(pageNum,pageSize);
            List<Productdetail> pro = dao.selectProductByOne(name);
            for (Productdetail p : pro) {
                double a = p.getPricenow();
                double b = p.getPricenum();
                double c = (a/b) * 100;
                int bfb = (int) c;
                p.setBfb(bfb);
            }
            PageInfo<Productdetail>  page = new PageInfo<>(pro);
            return page;
    }

    @Override
    public PageInfo<Productdetail> selectProductByTwo(int pageNum, int pageSize,String name) {
            PageHelper.startPage(pageNum,pageSize);
            List<Productdetail> pro = dao.selectProductByTwo(name);
            for (Productdetail p : pro) {
                double a = p.getPricenow();
                double b = p.getPricenum();
                double c = (a/b) * 100;
                int bfb = (int) c;
                p.setBfb(bfb);
            }
            PageInfo<Productdetail>  page = new PageInfo<>(pro);
            return page;
    }

    @Override
    public ProductLevel selectLevelById(long id){
        return dao.selectLevelById(id);
    }
}
